home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro5 / kernel.asm < prev    next >
Encoding:
Assembly Source File  |  1986-03-25  |  7.1 KB  |  181 lines

  1. ;
  2. ;    Terra Nova Communications multi-tasking kernel
  3. ;    Initialization and task-switcher
  4. ;
  5. ;    Note: this is not intended to be a complete listing.  It's only
  6. ;    a sample of some of the techniques used in our system.
  7. ;
  8.  
  9.         PSECT    Kernel
  10.     
  11. ;        External symbols (defined in other code segments)
  12.         EXTERN    VecTable,    ;vector table for hardware vector list
  13.             JMPTable,    ;jump table for system calls
  14.             JMPTabLen,    ;length of jump table in longwords
  15.             KernEnd,    ;end of kernel code item in heap
  16.             IOInit,        ;our private I/O initialization routine
  17.             HeapInit,    ;our private heap initialization
  18.             SysInit,    ;system variable initializer
  19.             SysConMon,    ;entry point for system console
  20.                     ;monitor task
  21.             HeapMunger,    ;entry point for heap munger task
  22.             DiskMunger    ;entry point for disk munger task
  23.  
  24. ;        Entry points in this module (referenced from elsewhere)
  25.         ENTRY   Start,        ;primary entry point to boot our OS
  26.             ConSwitch,    ;main context switcher
  27.             ConSwSleep    ;alternate context switcher (puts
  28.                     ;calling task to sleep)
  29.  
  30. ;        Include files (mostly equates)
  31.         INCLUDE    SysEqu        ;contains the low-memory absolute
  32.                     ;address equates (jump table, etc)
  33.         INCLUDE    HeapDef        ;defines the heap data structure
  34.         INCLUDE    SysIO        ;contains hardware I/O equates
  35.  
  36. ;        Miscellaneous storage
  37. CodeHeap    DS.L    8        ;heap header for kernel heap item
  38. StackEnd    DS.L    40        ;system stack before tasking starts
  39. StackBegin    DS.L    0        ;top of startup stack area
  40.  
  41. ;        Pre-tasking initialization
  42. ;        this code works in single-task mode
  43. ;        prior to the invocation of the context switcher
  44. Start            ;Initial entry.  Calling operating system is still
  45.              ;alive and kicking at this point.
  46. TakeOver    LEA    ReEntry,A1    ;point to re-entry instruction
  47.         MOVE.L    A1,$20.W    ;move short absolute to the vector
  48.                     ;for privilege exceptions
  49.         MOVE    USP,A0        ;try a privileged instruction.  If it
  50.             ;works, then we're in priv. mode. If not, then trap to
  51.              ;ReEntry and be in privileged mode anyway
  52. ReEntry        LEA    StackBegin,A7    ;set up initial stack
  53.  
  54. ;        Turn off all interrupts in the system
  55. ;        Note: this is device-specific code.
  56. ;        The labels in the operand fields are from our own
  57. ;        SysIO include file.
  58.         CLR.B    FDCIntMask    ;clear floppy disk & system console
  59.         CLR.B    HDIntMask    ;clear hard disk completion int. mask
  60.         CLR.B    SerIO1IntMask    ;clear serial boards
  61.         CLR.B    SerIO2IntMask
  62.  
  63. ;        Initialize the vector table
  64. ;        Copy the vectors from an assembled table (in another module) 
  65. ;        into the actual hardware vector list in low RAM
  66.         LEA    VecTable,A0    ;source (in another code segment)
  67.         LEA    $0.W,A1        ;destination (begins at $00 0000)
  68.         MOVE    #191,D7        ;192 longwords to move
  69. VecMove        MOVE.L    (A0)+,(A1)+    ;move a longword
  70.         DBRA    D7,VecMove    ;repeat till done (fast loop on 68010)
  71.  
  72. ;        Copy system routine JMP table from assembled object code (in
  73. ;        another module) to low memory jump table, where everyone
  74. ;        can get at them.
  75.         LEA    JMPTable,A0    ;source
  76.         LEA    System.W,A1    ;dest. (name of first system call in 
  77.             ;the jump table.  "System" is from the SysEqu include
  78.              ;file.  It's the context switcher)
  79.         MOVE    #JMPTabLen/4,D7    ;number of longwords to move
  80. JPTMove        MOVE.L    (A0)+,(A1)+    ;move a longword
  81.         DBRA    D7,JPTMove    ;repeat till done (fast loop on 68010)
  82.  
  83. ;        Clear low memory to zero (between jmp table and kernel)
  84.         LEA    StackEnd,A1    ;point to top of destination
  85.             ;and bottom of destination (end of the jump table)
  86.         LEA    System+JMPTabLen.W,A0
  87.         SUBA    A0,A1        ;calculate the length
  88.         MOVE.L    A1,D7        ;move to D7 for counting
  89.         LSR.L    #4,D7         ;divide by 16 for 16-byte blocks
  90. LowClr        CLR.L    (A0)+        ;clear 16 bytes, quickly
  91.         CLR.L    (A0)+
  92.         CLR.L    (A0)+
  93.         CLR.L    (A0)+
  94.         DBRA    D7,LowClr    ;do it until done.
  95.  
  96. ;        Clear high memory to zero (between kernel and end of RAM)
  97. ;        (RAMEnd is first byte beyond RAM, defined in SysEqu)
  98.         LEA    RAMEnd,A1    ;point to top of destination
  99.             ;and bottom of destination (end of the jump table)
  100.         LEA    KernEnd,A0
  101.         SUBA    A0,A1        ;calc the length
  102.         MOVE.L    A1,D7        ;move to D7 for counting
  103.         LSR.L    #4,D7         ;divide by 16 for 16-byte blocks
  104. HiClr        CLR.L    (A0)+        ;clear 16 bytes, quickly
  105.         CLR.L    (A0)+
  106.         CLR.L    (A0)+
  107.         CLR.L    (A0)+
  108.         DBRA    D7,HiClr    ;do it until done.
  109.  
  110. ;        Initialize all of the primary I/O devices
  111. ;        Note: this is a device specific routine not treated in the article.
  112.         JSR    IOInit
  113.     
  114. ;        Initialize the heap
  115. ;        Note: this is a routine in the heap manager, which creates
  116. ;        valid heap headers for the three initial heap items discussed
  117. ;        in the text: the deletion below the kernel, the kernel code
  118. ;        item, and the deletion above the kernel.
  119.         JSR    HeapInit
  120.  
  121. ;        Initialize the system zone of low memory
  122. ;        Note: this sets up the TCB and master handle arrays, as
  123. ;        discussed in the text, as well as initializing the time of day 
  124. ;        and the date and the other miscellaneous system values.
  125.         JSR    SysInit
  126.  
  127. ;        Spawn off the initial tasks
  128. ;        This will create TCBs and TData items for the tasks, but won't
  129. ;        invoke them.  They're invoked only by the context switcher.
  130.         LEA    SysConMon,A0    ;point to system console entry point
  131.         MOVE.L    #4096,D0    ;tell it how much RAM for TData
  132.         JSR    Spawn        ;jump through jump table entry
  133.             ;("Spawn" is a jump table equate in SysEqu)
  134.         LEA    HeapMunger,A0    ;spawn the heap munger
  135.         MOVE.L    #512,D0        ;heap munger's TData size
  136.         JSR    Spawn
  137.         LEA    DiskMunger,A0    ;Spawn the disk munger
  138.         MOVE.L    #8192,D0    ;(TData includes one disk buffer)
  139.         JSR    Spawn
  140.  
  141.         LEA    TCB1.W,A2    ;get address of first TCB in array
  142.                     ;(TCB1 is defined in SysEqu)
  143.         BRA.S    ConSw1        ;now start the context switcher!
  144.  
  145. ;        Context Switcher: primary version
  146. ;        Simple task-switch, nothing fancy.
  147. ;        SysFlags is a low-RAM system flag byte, defined in SysEqu.
  148. ;        The data structure for the TData item is defined in SysEqu.
  149. ;        The data structure for the TCB is defined in SysEqu.
  150. ConSwitch    BTST    #StopSys,SysFlags.W ;task switching inhibited?
  151.         BNE.S    ConSwX        ;yes, exit back to caller
  152.         MOVE.L    OurTCB(A5),A0    ;get TCB address from TData
  153.         SUBA.L    A5,SP        ;subtract TData base addr from stack
  154.         MOVE.L    SP,TCBSP(A0)    ;save relative displacement in TCB
  155.  
  156.         MOVE.L    TCBNxt(A0),A2    ;get address of next TCB
  157. ConSw1        MOVE.L    TCBA5(A2),A5    ;get new TData base address
  158.         MOVE.L    TCBSP(A2),SP    ;get stack relative displacement
  159.         ADDA.L    A5,SP        ;restore absolute address
  160. ConSwX        RTS            ;return to next task
  161.  
  162. ;        Context Switcher: alternate version
  163. ;        Put the calling task to sleep.
  164. ConSwSleep    BTST    #StopSys,SysFlags.W ;task switching inhibited?
  165.         BNE.S    ConSwX        ;yes, exit back to caller
  166.                     ;without going to sleep
  167.         MOVE.L    OurTCB(A5),A0    ;get TCB address from TData
  168.         SUBA.L    A5,SP        ;subtract TData base addr from stack
  169.         MOVE.L    SP,TCBSP(A0)    ;save relative displacement in TCB
  170.  
  171.         MOVE.L    TCBNxt(A0),A2    ;get address of next TCB
  172.         MOVE.L    TCBPrev(A0),A1     ;get addr of previous TCB
  173.         MOVE.L    A2,TCBNxt(A1)    ;close the pointers around the now-
  174.         MOVE.L    A1,TCBPrev(A2)     ;sleeping task.
  175.         MOVE.B    #Sleep,TCBState(A0) ;mark it as asleep
  176.  
  177.         MOVE.L    TCBA5(A2),A5    ;get new TData base address
  178.         MOVE.L    TCBSP(A2),SP    ;get stack relative displacement
  179.         ADDA.L    A5,SP        ;restore absolute address
  180.         RTS            ;return to next task
  181.